#include "qspi.h"
#include "delay.h"

QSPI_HandleTypeDef QSPI_Handler;    //QSPI句柄

#define STM_HAL
#define delaytime 0
u8 dataMode_FLASH = 0;
//QSPI初始化
u8 QSPI_Init(void)
{
    QSPI_Handler.Instance=QUADSPI;                          //QSPI
    #ifdef STM_HAL
    
    QSPI_Handler.Init.ClockPrescaler=5;                     //QPSI分频比，W25N01最大频率为104M，
                                                            //所以此处应该为2，QSPI频率就为216/(2+1)=72MHZ   216/(5+1)=36MHZ
    QSPI_Handler.Init.FifoThreshold=4;                      //FIFO阈值为4个字节
    QSPI_Handler.Init.SampleShifting=QSPI_SAMPLE_SHIFTING_HALFCYCLE;//采样移位半个周期(DDR模式下,必须设置为0)
    QSPI_Handler.Init.FlashSize=POSITION_VAL(0x08000000)-1;  //SPI FLASH大小，W25Q256大小为128M字节
    QSPI_Handler.Init.ChipSelectHighTime=QSPI_CS_HIGH_TIME_8_CYCLE;//片选高电平时间为4个时钟(13.8*4=55.2ns),即手册里面的tSHSL参数
    QSPI_Handler.Init.ClockMode=QSPI_CLOCK_MODE_0;          //模式0
    QSPI_Handler.Init.FlashID=QSPI_FLASH_ID_1;              //第一片flash
    QSPI_Handler.Init.DualFlash=QSPI_DUALFLASH_DISABLE;     //禁止双闪存模式
    if(HAL_QSPI_Init(&QSPI_Handler)==HAL_OK) 
		return 0;      //QSPI初始化成功
    else 
		return 1;
    #else
    if(HAL_QSPI_Init(&QSPI_Handler)==HAL_OK) 
		{
			////IO test  function,保持各IO都是高电平后在离开此处。
			QPI_FLASH_CS_L;
			QPI_FLASH_CS_H;
			QPI_FLASH_IO0_L;
			QPI_FLASH_IO0_H;
			QPI_FLASH_IO1_L;
			QPI_FLASH_IO1_H;
			QPI_FLASH_IO2_L;
			QPI_FLASH_IO2_H;
			QPI_FLASH_IO3_L;
			QPI_FLASH_IO3_H;
			return 0;      //QSPI初始化成功
		}
    else 
		return 1;
    #endif
}

//QSPI底层驱动,引脚配置，时钟使能
//此函数会被HAL_QSPI_Init()调用
//hqspi:QSPI句柄
void HAL_QSPI_MspInit(QSPI_HandleTypeDef *hqspi)
{
    GPIO_InitTypeDef GPIO_Initure;
#ifdef STM_HAL   
    __HAL_RCC_QSPI_CLK_ENABLE();        //使能QSPI时钟
    __HAL_RCC_GPIOB_CLK_ENABLE();       //使能GPIOB时钟
    __HAL_RCC_GPIOF_CLK_ENABLE();       //使能GPIOF时钟
    
    
    //初始化PB6 片选信号
    GPIO_Initure.Pin=GPIO_PIN_6;
    GPIO_Initure.Mode=GPIO_MODE_AF_PP;          //复用
    GPIO_Initure.Pull=GPIO_NOPULL;              
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;         //高速
    GPIO_Initure.Alternate=GPIO_AF10_QUADSPI;   //复用为QSPI
    HAL_GPIO_Init(GPIOB,&GPIO_Initure);
    
    //PF8,9
    GPIO_Initure.Pin=GPIO_PIN_8|GPIO_PIN_9;
    GPIO_Initure.Mode=GPIO_MODE_AF_PP;          //复用
    GPIO_Initure.Pull=GPIO_NOPULL; //GPIO_NOPULL;              
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;         //高速
    GPIO_Initure.Alternate=GPIO_AF10_QUADSPI;   //复用为QSPI
    HAL_GPIO_Init(GPIOF,&GPIO_Initure);
    
//    //PF10
//    GPIO_Initure.Pin=GPIO_PIN_10;
//    GPIO_Initure.Alternate=GPIO_AF9_QUADSPI;   //复用为QSPI
//    HAL_GPIO_Init(GPIOF,&GPIO_Initure);
    
    //PF6,7
    GPIO_Initure.Pin=GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_10;
    GPIO_Initure.Mode=GPIO_MODE_AF_PP;          //复用
    GPIO_Initure.Pull=GPIO_NOPULL;              
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;         //高速
    GPIO_Initure.Alternate=GPIO_AF9_QUADSPI;   //复用为QSPI
    HAL_GPIO_Init(GPIOF,&GPIO_Initure);
    
#else
    
		#ifdef FLASH_MODULE_USE_ONLY_V02
		__HAL_RCC_GPIOD_CLK_ENABLE();       //使能GPIOD时钟
		__HAL_RCC_GPIOE_CLK_ENABLE();       //使能GPIOE时钟
		__HAL_RCC_GPIOG_CLK_ENABLE();       //使能GPIOG时钟
		
		//初始化PB6 片选信号   CS0  PE4
    GPIO_Initure.Pin=GPIO_PIN_4;
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;          //复用
    GPIO_Initure.Pull=GPIO_PULLUP;              
    GPIO_Initure.Speed=GPIO_SPEED_LOW;         //低速
    HAL_GPIO_Init(GPIOE,&GPIO_Initure);
    
////    //PD12    SCK
    GPIO_Initure.Pin=GPIO_PIN_12;
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;          
    GPIO_Initure.Pull=GPIO_PULLUP;              
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;         //高速
    HAL_GPIO_Init(GPIOD,&GPIO_Initure);

    ////PE3      ---IO1  ---SDO
    GPIO_Initure.Pin=GPIO_PIN_3;
    GPIO_Initure.Mode=GPIO_MODE_INPUT;          //
    GPIO_Initure.Pull=GPIO_PULLUP;              
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;         //高速
    HAL_GPIO_Init(GPIOE,&GPIO_Initure);
    //PD13        ----IO0-----SDI
    GPIO_Initure.Pin=GPIO_PIN_13;
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;          
    GPIO_Initure.Pull=GPIO_PULLUP; //GPIO_NOPULL;              
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;         //高速
    HAL_GPIO_Init(GPIOD,&GPIO_Initure);
		
		////PD11      ---IO2  ---WP
    GPIO_Initure.Pin=GPIO_PIN_11;
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;          //
    GPIO_Initure.Pull=GPIO_PULLUP;              
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;         //高速
    HAL_GPIO_Init(GPIOD,&GPIO_Initure);
		
		////PG13      ---IO3  ---HOLD
    GPIO_Initure.Pin=GPIO_PIN_13;
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;          //
    GPIO_Initure.Pull=GPIO_PULLUP;              
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;         //高速
    HAL_GPIO_Init(GPIOG,&GPIO_Initure);
		
		#else
//    __HAL_RCC_QSPI_CLK_ENABLE();        //使能QSPI时钟
    __HAL_RCC_GPIOB_CLK_ENABLE();       //使能GPIOB时钟
    __HAL_RCC_GPIOF_CLK_ENABLE();       //使能GPIOF时钟
    
    
    //初始化PB6 片选信号   CS0
    GPIO_Initure.Pin=GPIO_PIN_6;
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;          //复用
    GPIO_Initure.Pull=GPIO_NOPULL;              
    GPIO_Initure.Speed=GPIO_SPEED_LOW;         //低速
    HAL_GPIO_Init(GPIOB,&GPIO_Initure);
    
////    //PF10    SCK
    GPIO_Initure.Pin=GPIO_PIN_10;
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;          
    GPIO_Initure.Pull=GPIO_NOPULL;              
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;         //高速
    HAL_GPIO_Init(GPIOF,&GPIO_Initure);

    ////PF9      ---IO1  ---SDO
    GPIO_Initure.Pin=GPIO_PIN_9;
    GPIO_Initure.Mode=GPIO_MODE_INPUT;          //
    GPIO_Initure.Pull=GPIO_NOPULL;              
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;         //高速
    HAL_GPIO_Init(GPIOF,&GPIO_Initure);
    //PF8        ----IO0-----SDI
    GPIO_Initure.Pin=GPIO_PIN_8;
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_OD;          
    GPIO_Initure.Pull=GPIO_NOPULL; //GPIO_NOPULL;              
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;         //高速
    HAL_GPIO_Init(GPIOF,&GPIO_Initure);
    
    ////PF7
//    GPIO_Initure.Pin=GPIO_PIN_7;
//    GPIO_Initure.Mode=GPIO_MODE_INPUT;          //
//    GPIO_Initure.Pull=GPIO_NOPULL;              
//    GPIO_Initure.Speed=GPIO_SPEED_HIGH;         //高速
//    HAL_GPIO_Init(GPIOF,&GPIO_Initure);
//    
//    //PF6
//    GPIO_Initure.Pin=GPIO_PIN_6;
//    GPIO_Initure.Mode=GPIO_MODE_INPUT;          
//    GPIO_Initure.Pull=GPIO_NOPULL;              
//    GPIO_Initure.Speed=GPIO_SPEED_HIGH;         //高速
//    HAL_GPIO_Init(GPIOF,&GPIO_Initure);
	#endif
#endif
}
#ifdef STM_HAL
#else

void QPI_PROGRAM_IO_INIT()
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

		#ifdef FLASH_MODULE_USE_ONLY_V02
		GPIO_InitStruct.Pin = GPIO_PIN_13;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
		
		GPIO_InitStruct.Pin = GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
		
		GPIO_InitStruct.Pin = GPIO_PIN_13;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
		#else
    GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_6|GPIO_PIN_8|GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
		#endif

}
void QPI_READOUT_IO_INIT()
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

	#ifdef FLASH_MODULE_USE_ONLY_V02
		GPIO_InitStruct.Pin = GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
		
//		GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_11;
//    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
//    GPIO_InitStruct.Pull = GPIO_NOPULL;
//    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
//    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
//		
//		GPIO_InitStruct.Pin = GPIO_PIN_13;
//    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
//    GPIO_InitStruct.Pull = GPIO_NOPULL;
//    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
//    HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
		#else
    GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_6|GPIO_PIN_8|GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
	#endif
}
void QPI_DELAY(int a)
{
	for(int i=0;i<a;i++)
	{
	for (int j=0;j<100;j++)
		{}
	}
}
void QPI_WRITE_INSTRUCTION(uint8_t instruction)
{
	for(int i=0;i<8;i++)
    {
		QPI_FLASH_SCK_L;
		//QPI_DELAY(delaytime);
		if((instruction&0x80)==0x80)
        {
			QPI_FLASH_IO0_H;
		}
		else
        {
			QPI_FLASH_IO0_L;
		}
			QPI_DELAY(delaytime);
		QPI_FLASH_SCK_H;
		QPI_DELAY(delaytime);
		instruction<<=1;
	}
}
void QPI_WRITE_DUMMY(uint8_t instruction)
{
	for(int i=0;i<instruction;i++)
    {
		QPI_FLASH_SCK_L;
		QPI_DELAY(delaytime);
		if((instruction&0x80)==0x80)
        {
			QPI_FLASH_IO0_H;
		}
		else
        {
			QPI_FLASH_IO0_L;
		}
			QPI_DELAY(delaytime);
		QPI_FLASH_SCK_H;
		QPI_DELAY(delaytime);
		instruction<<=1;
	}
}
#endif

//QSPI发送命令
//instruction:要发送的指令
//address:发送到的目的地址
//dummyCycles:空指令周期数
//instructionMode:指令模式;QSPI_INSTRUCTION_NONE,QSPI_INSTRUCTION_1_LINE,QSPI_INSTRUCTION_2_LINE,QSPI_INSTRUCTION_4_LINE  
//addressMode:地址模式; QSPI_ADDRESS_NONE,QSPI_ADDRESS_1_LINE,QSPI_ADDRESS_2_LINE,QSPI_ADDRESS_4_LINE
//addressSize:地址长度;QSPI_ADDRESS_8_BITS,QSPI_ADDRESS_16_BITS,QSPI_ADDRESS_24_BITS,QSPI_ADDRESS_32_BITS
//dataMode:数据模式; QSPI_DATA_NONE,QSPI_DATA_1_LINE,QSPI_DATA_2_LINES,QSPI_DATA_4_LINES

void QSPI_Send_CMD(u32 instruction,u32 address,u32 dummyCycles,u32 instructionMode,u32 addressMode,u32 addressSize,u32 dataMode)
{
    QSPI_CommandTypeDef Cmdhandler;
    
    #ifdef STM_HAL
    Cmdhandler.Instruction=instruction;                 	//指令
    Cmdhandler.Address=address;                            	//地址
    Cmdhandler.DummyCycles=dummyCycles;                     //设置空指令周期数
    Cmdhandler.InstructionMode=instructionMode;				//指令模式
    Cmdhandler.AddressMode=addressMode;   					//地址模式
    Cmdhandler.AddressSize=addressSize;   					//地址长度
    Cmdhandler.DataMode=dataMode;             				//数据模式
    Cmdhandler.SIOOMode=QSPI_SIOO_INST_EVERY_CMD;       	//每次都发送指令
    Cmdhandler.AlternateByteMode=QSPI_ALTERNATE_BYTES_NONE; //无交替字节
    Cmdhandler.DdrMode=QSPI_DDR_MODE_DISABLE;           	//关闭DDR模式
    Cmdhandler.DdrHoldHalfCycle=QSPI_DDR_HHC_ANALOG_DELAY;

    HAL_QSPI_Command(&QSPI_Handler,&Cmdhandler,5000);
//    printf("\n\t instruction= 0x%x ;address = 0x%x;dummyCycles= %d;addressSize= %d;",instruction,address,dummyCycles,addressSize);
    #else
    QPI_FLASH_CS_L;
    if(instructionMode == QSPI_INSTRUCTION_1_LINE)
    {
        //instruction;
        QPI_WRITE_INSTRUCTION(instruction&0xFF);
    }
    if(dummyCycles != 0)
    {
        ///dummyCycles
        QPI_WRITE_DUMMY(dummyCycles);
    }
    
    if((addressMode == QSPI_ADDRESS_1_LINE)&&(address != 0))
    {
        ///address
        switch(addressSize)
        {
            case QSPI_ADDRESS_8_BITS:
                QPI_WRITE_INSTRUCTION(address&0xFF);
                break;
            case QSPI_ADDRESS_16_BITS:
                QPI_WRITE_INSTRUCTION((address&0x0000FF00)>>8);
                QPI_WRITE_INSTRUCTION((address&0x000000FF)>>0);
                break;
            case QSPI_ADDRESS_24_BITS:
                QPI_WRITE_INSTRUCTION((address&0x00FF0000)>>16);
                QPI_WRITE_INSTRUCTION((address&0x0000FF00)>>8);
                QPI_WRITE_INSTRUCTION((address&0x000000FF)>>0);
                break;
            case QSPI_ADDRESS_32_BITS:
                QPI_WRITE_INSTRUCTION((address&0xFF000000)>>24);
                QPI_WRITE_INSTRUCTION((address&0x00FF0000)>>16);
                QPI_WRITE_INSTRUCTION((address&0x0000FF00)>>8);
                QPI_WRITE_INSTRUCTION((address&0x000000FF)>>0);
                break;
            default :
              //  QPI_WRITE_INSTRUCTION(address&0xFF);
                break;
        }
    }
    switch(dataMode)
    {
        case QSPI_DATA_NONE:
            dataMode_FLASH=0;
                QPI_FLASH_CS_H;
            break;
        case QSPI_DATA_1_LINE:
//            HAL_QSPI_MspInit(&QSPI_Handler);
            dataMode_FLASH=1;
            break;
        case QSPI_DATA_2_LINES:
            break;
        case QSPI_DATA_4_LINES:
            
            dataMode_FLASH = 4;
            break;
        default :
            dataMode_FLASH=1;
                break;        
    }

    #endif
}

//QSPI接收指定长度的数据
//buf:接收数据缓冲区首地址
//datalen:要传输的数据长度
//返回值:0,正常
//    其他,错误代码
u8 QSPI_Receive(u8* buf,u32 datalen)
{
    #ifdef STM_HAL
    QSPI_Handler.Instance->DLR=datalen-1;                           //配置数据长度
    if(HAL_QSPI_Receive(&QSPI_Handler,buf,5000)==HAL_OK) return 0;  //接收数据
    else return 1;
    #else
    uint8_t rec=0;
    if(dataMode_FLASH == 4)
    {
        QPI_READOUT_IO_INIT();
        for(int i=0;i<datalen;i++)
        {
            QPI_FLASH_SCK_L;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);

            if(HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_6)==1)
            {  //io3
                rec+=1;
            }
            rec<<=1;
            if(HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_7)==1)
            {  //io2
                rec+=1;
            }
            rec<<=1;
            if(HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_9)==1)
            {  //io1
                rec+=1;
            }
            rec<<=1;
            if(HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_8)==1)
            {  //io0
                rec+=1;
            }
                rec<<=1;
            QPI_FLASH_SCK_L;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            if(HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_6)==1)
            {  //io3
                rec+=1;
            }
            rec<<=1;
            if(HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_7)==1)
            {  //io2
                rec+=1;
            }
            rec<<=1;
            if(HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_9)==1)
            {  //io1
                rec+=1;
            }
            rec<<=1;
            if(HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_8)==1)
            {  //io0
                rec+=1;
            }
            buf[i]=rec;
            rec=0;
            QPI_DELAY(delaytime);
        }
    }
	else if(dataMode_FLASH == 1)
    {
			QPI_READOUT_IO_INIT();
//        HAL_QSPI_MspInit(&QSPI_Handler);
        for(int i=0;i<datalen;i++)
        {
            QPI_FLASH_SCK_L;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            if(HAL_GPIO_ReadPin(QPI_FLASH_IO1_GPIO, QPI_FLASH_IO1_PIN)==1)
            {  //io0
                rec+=1;
            }
                rec<<=1;
            
            QPI_FLASH_SCK_L;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            if(HAL_GPIO_ReadPin(QPI_FLASH_IO1_GPIO, QPI_FLASH_IO1_PIN)==1)
            {  //io0
                rec+=1;
            }
                rec<<=1;
            
            QPI_FLASH_SCK_L;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            if(HAL_GPIO_ReadPin(QPI_FLASH_IO1_GPIO, QPI_FLASH_IO1_PIN)==1)
            {  //io0
                rec+=1;
            }
                rec<<=1;
            
            QPI_FLASH_SCK_L;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            if(HAL_GPIO_ReadPin(QPI_FLASH_IO1_GPIO, QPI_FLASH_IO1_PIN)==1)
            {  //io0
                rec+=1;
            }
                rec<<=1;
            
            QPI_FLASH_SCK_L;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            if(HAL_GPIO_ReadPin(QPI_FLASH_IO1_GPIO, QPI_FLASH_IO1_PIN)==1)
            {  //io0
                rec+=1;
            }
                rec<<=1;
            
            QPI_FLASH_SCK_L;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            if(HAL_GPIO_ReadPin(QPI_FLASH_IO1_GPIO, QPI_FLASH_IO1_PIN)==1)
            {  //io0
                rec+=1;
            }
                rec<<=1;
            
            QPI_FLASH_SCK_L;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            if(HAL_GPIO_ReadPin(QPI_FLASH_IO1_GPIO, QPI_FLASH_IO1_PIN)==1)
            {  //io0
                rec+=1;
            }
                rec<<=1;
            
            
            QPI_FLASH_SCK_L;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            if(HAL_GPIO_ReadPin(QPI_FLASH_IO1_GPIO, QPI_FLASH_IO1_PIN)==1)
            {  //io0
                rec+=1;
            }
            buf[i]=rec;
            rec=0;
            QPI_DELAY(delaytime);
        }
    }
        else
        {}
			QPI_FLASH_CS_H;
			HAL_QSPI_MspInit(&QSPI_Handler);
    
    #endif
}

//QSPI发送指定长度的数据
//buf:发送数据缓冲区首地址
//datalen:要传输的数据长度
//返回值:0,正常
//    其他,错误代码
u8 QSPI_Transmit(u8* buf,u32 datalen)
{
    #ifdef STM_HAL
    QSPI_Handler.Instance->DLR=datalen-1;                            //配置数据长度
    if(HAL_QSPI_Transmit(&QSPI_Handler,buf,5000)==HAL_OK) return 0;  //发送数据
    else return 1;
    #else
    uint8_t temp;
    if(dataMode_FLASH == 4)
    {
        QPI_PROGRAM_IO_INIT();
        
        for(int i=0;i<datalen;i++)
        {
            QPI_FLASH_SCK_L;
        
            temp=buf[i];
            if((temp&0x80)==0x80){QPI_FLASH_IO3_H ;}else {QPI_FLASH_IO3_L ;}temp<<=1;
            if((temp&0x80)==0x80){QPI_FLASH_IO2_H ;}else {QPI_FLASH_IO2_L ;}temp<<=1;
            if((temp&0x80)==0x80){QPI_FLASH_IO1_H ;}else {QPI_FLASH_IO1_L ;}temp<<=1;	
            if((temp&0x80)==0x80){QPI_FLASH_IO0_H ;}else {QPI_FLASH_IO0_L ;}temp<<=1;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_L;
        
            if((temp&0x80)==0x80){QPI_FLASH_IO3_H ;}else {QPI_FLASH_IO3_L ;}temp<<=1;
            if((temp&0x80)==0x80){QPI_FLASH_IO2_H ;}else {QPI_FLASH_IO2_L ;}temp<<=1;
            if((temp&0x80)==0x80){QPI_FLASH_IO1_H ;}else {QPI_FLASH_IO1_L ;}temp<<=1;	
            if((temp&0x80)==0x80){QPI_FLASH_IO0_H ;}else {QPI_FLASH_IO0_L ;}
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
        }
    }
    else if(dataMode_FLASH == 1)
    {
        for(int i=0;i<datalen;i++)
        {
            QPI_FLASH_SCK_L;
        
            temp=buf[i];
            if((temp&0x80)==0x80){QPI_FLASH_IO0_H ;}else {QPI_FLASH_IO0_L ;}temp<<=1;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_L;
            
            if((temp&0x80)==0x80){QPI_FLASH_IO0_H ;}else {QPI_FLASH_IO0_L ;}temp<<=1;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_L;
            
            if((temp&0x80)==0x80){QPI_FLASH_IO0_H ;}else {QPI_FLASH_IO0_L ;}temp<<=1;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_L;
            
            if((temp&0x80)==0x80){QPI_FLASH_IO0_H ;}else {QPI_FLASH_IO0_L ;}temp<<=1;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_L;
            
            if((temp&0x80)==0x80){QPI_FLASH_IO0_H ;}else {QPI_FLASH_IO0_L ;}temp<<=1;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_L;
            
            if((temp&0x80)==0x80){QPI_FLASH_IO0_H ;}else {QPI_FLASH_IO0_L ;}temp<<=1;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_L;
            
            if((temp&0x80)==0x80){QPI_FLASH_IO0_H ;}else {QPI_FLASH_IO0_L ;}temp<<=1;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_L;
            
            
        
//            if((temp&0x80)==0x80){QPI_FLASH_IO3_H ;}else {QPI_FLASH_IO3_L ;}temp<<=1;
//            if((temp&0x80)==0x80){QPI_FLASH_IO2_H ;}else {QPI_FLASH_IO2_L ;}temp<<=1;
//            if((temp&0x80)==0x80){QPI_FLASH_IO1_H ;}else {QPI_FLASH_IO1_L ;}temp<<=1;	
            if((temp&0x80)==0x80){QPI_FLASH_IO0_H ;}else {QPI_FLASH_IO0_L ;}
            QPI_DELAY(delaytime);
            QPI_FLASH_SCK_H;
            QPI_DELAY(delaytime);
        }
    }
        else
        {}
            HAL_QSPI_MspInit(&QSPI_Handler);
    #endif
}
